package com.bw.zg3park2212.signlist

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.entity.SiginParam
import com.bw.base.mvvm.BaseActivity
import com.bw.base.mvvm.BaseViewActivity
import com.bw.zg3park2212.R
import com.bw.zg3park2212.databinding.ActivitySignBinding
import com.bw.zg3park2212.signlist.SiginViewModel
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat
import java.util.Timer
import kotlin.concurrent.schedule
import kotlin.concurrent.timerTask

@AndroidEntryPoint
@Route(path="/app/SignActivity")
class SignActivity : BaseActivity<ActivitySignBinding,SiginViewModel>() {
    lateinit var timer:Timer
    override fun intitView() {
        timer= Timer()
        lifecycleScope.launch {
            timer.schedule(timerTask {
                lifecycleScope.launch(Dispatchers.Main){
                    binding.tvTime.setText(SimpleDateFormat("HH:mm:ss").format(System.currentTimeMillis()))
                }
            },0,1000 )      }
        binding.btnSign.setOnClickListener {
            var siginParam=SiginParam()
            var staute="正常"
            var datatime=SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(System.currentTimeMillis())
            val split = datatime.split(" ")
            if (binding.tvSignText.text=="签到"){
                siginParam.morningCheckIn=datatime
                binding.tvSignText.text="签退"
                if (split[1]>"09:00:00"){
                    staute="迟到"
                    binding.signStartTime.text=split[1]+"("+staute+")"
                }
                siginParam.status=staute
                viewmodel.getsigindaka(siginParam)
            }else if (binding.tvSignText.text=="签退"){
                siginParam.afternoonCheckIn=datatime
                binding.tvSignText.text="结束签到"
                binding.btnSign.setBackgroundResource(R.drawable.bg_hui)
                binding.signEndTime.text=split[1]
                if(split[1]<"18:00:00"){
                    staute="早退"
                    binding.signEndTime.text=split[1]+"("+staute+")"
                }
                siginParam.status=staute
                viewmodel.getsigindaka(siginParam)
            }
        }
    }

    override fun intitData() {
        viewmodel.siginDakaSuccse.observe(this){
            ToastUtils.showLong("打卡成功")
        }
    }


}